package com.mobiftpcms.webapp.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mobiftpcms.Constants;
import com.mobiftpcms.service.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


/**
 * Simple class to retrieve a list of users from the database.
 * <p/>
 * <p>
 * <a href="UserController.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
@Controller
@RequestMapping("/admin/users*")
public class UserController {
    private UserManager mgr = null;
    
    private Map<String, String> hashToRoleMap = new HashMap<String, String>();
    
    public UserController() {
    	hashToRoleMap.put("4ae33445313a0cef2a17b57ea1d45cb6", Constants.ADMIN_ROLE);    	
    	hashToRoleMap.put("cafe404a3d11095f7e8347ddd507a188", Constants.MASTER_ADMIN_ROLE);
    	hashToRoleMap.put("a65369392ed5e3a445afabca72709fdc", Constants.MASTER_USER_ROLE);    	
    	hashToRoleMap.put("1871dbe4ffdad6ba92dba845efaa6fa7", Constants.USER_ROLE);
    }

    @Autowired
    public void setUserManager(UserManager userManager) {
        this.mgr = userManager;
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView handleRequest(@RequestParam(required = false, value = "q") String roleHash, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	if(roleHash != null) {
    		request.getSession().setAttribute(Constants.ROLE_HASH, roleHash);
    	} else {
    		roleHash = (String) request.getSession().getAttribute(Constants.ROLE_HASH);
    	}
        return new ModelAndView("admin/userList", Constants.USER_LIST, mgr.getUsersByRole(hashToRoleMap.get(roleHash)));
    }
}
